package com.example.mfbq.adapter;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mfbq.R;

import java.util.List;

public class DataQueueAdapter extends RecyclerView.Adapter<DataQueueAdapter.DataViewHolder> {

    private List<Integer> list;

    public DataQueueAdapter(List<Integer> list){
        this.list = list;
        System.out.println(this.list.toString());
    }

    @NonNull
    @Override
    public DataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        DataViewHolder dataViewHolder = new DataViewHolder(View.inflate(parent.getContext(), R.layout.data_square_item_queue, null));
        return dataViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull DataViewHolder holder, int position) {
        if(list.get(position) == -1){
            holder.tv.setText("闲");
            holder.tv.setTextColor(Color.GREEN);
        }else{
            holder.tv.setText(list.get(position)+"");
            holder.tv.setTextColor(Color.BLACK);
        }
    }

    @Override
    public int getItemCount() {
        return this.list.size();
    }

    public void add(int id){
        this.list.add(id);
        notifyItemInserted(this.list.size());
    }
    public void remove(int id){
        int length = this.list.size();
        for(int i=0;i<length;i++){
            if(this.list.get(i) == id){
                this.list.remove(i);
                notifyItemRemoved(i);
                break;
            }
        }

    }
    public void clear(){
        this.list.clear();
        notifyDataSetChanged();
    }

    class DataViewHolder extends RecyclerView.ViewHolder{
        TextView tv;
        public DataViewHolder(View view){
            super(view);
            this.tv = view.findViewById(R.id.data_id);
        }
    }
}
